package com.zhiche.lisa.lspm.service.attach.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import com.zhiche.lisa.core.utils.qiniu.config.QiniuConfig;
import com.zhiche.lisa.core.utils.qiniu.util.QiniuUtils;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicense;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.driver.DriverLicense;
import com.zhiche.lisa.lspm.dao.model.driver.DriverLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.lsp.LspLicense;
import com.zhiche.lisa.lspm.dao.model.lsp.LspLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerLicense;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerLicenseAttach;
import com.zhiche.lisa.lspm.service.attach.AttachService;
import com.zhiche.lisa.lspm.service.carrier.ICarrierLicenseAttachService;
import com.zhiche.lisa.lspm.service.carrier.ICarrierLicenseService;
import com.zhiche.lisa.lspm.service.driver.IDriverLicenseAttachService;
import com.zhiche.lisa.lspm.service.driver.IDriverLicenseService;
import com.zhiche.lisa.lspm.service.lsp.LspLicenseAttachService;
import com.zhiche.lisa.lspm.service.lsp.LspLicenseService;
import com.zhiche.lisa.lspm.service.trailer.TrailerLicenseAttachService;
import com.zhiche.lisa.lspm.service.trailer.TrailerLicenseService;
import com.zhiche.lisa.lspm.vo.attach.AttachDetailVO;
import com.zhiche.lisa.lspm.vo.attach.LicenseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AttachServiceImpl implements AttachService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AttachServiceImpl.class);
    private String DRIVER = "driver";
    private String LSP = "lsp";
    private String TRAILER = "trailer";
    private String CARRIER = "carrier";


    @Autowired
    private LspLicenseService lspLicenseService;
    @Autowired
    private LspLicenseAttachService lspLicenseAttachService;
    @Autowired
    private IDriverLicenseService driverLicenseService;
    @Autowired
    private IDriverLicenseAttachService driverLicenseAttachService;
    @Autowired
    private ICarrierLicenseService carrierLicenseService;
    @Autowired
    private ICarrierLicenseAttachService carrierLicenseAttachService;
    @Autowired
    private TrailerLicenseService trailerLicenseService;
    @Autowired
    private TrailerLicenseAttachService trailerLicenseAttachService;
    @Autowired
    private QiniuConfig qiniuConfig;
    /**
     * 获取附件
     * @param licenseVO 证照类型，所属的承运商或司机等的id和标识
     * @return
     */
    @Override
    public List<AttachDetailVO> queryAttachList(LicenseVO licenseVO) {
        List<AttachDetailVO> attachDetailVOS = new ArrayList<>();
        try {
            //承运商
            if (LSP.equals(licenseVO.getBelongedType())) {
                //查找证照信息
                EntityWrapper<LspLicense> lspLicenseEntityWrapper = new EntityWrapper<>();
                lspLicenseEntityWrapper.eq("lsp_id", licenseVO.getBelongedId()).eq("license_type_id", licenseVO.getLicenseTypeId());
                List<LspLicense> lspLicenses = lspLicenseService.selectList(lspLicenseEntityWrapper);
                if (!CollectionUtils.isEmpty(lspLicenses)) {
                    //查找旗下的附件信息
                    EntityWrapper<LspLicenseAttach> lspLicenseAttachEntityWrapper = new EntityWrapper<>();
                    lspLicenseAttachEntityWrapper.eq("license_id", lspLicenses.get(0).getId()).eq("lsp_id", licenseVO.getBelongedId());
                    List<LspLicenseAttach> lspLicenseAttaches = lspLicenseAttachService.selectList(lspLicenseAttachEntityWrapper);
                    //遍历附件，绑定文件附属信息
                    for (LspLicenseAttach lspLicenseAttach :
                            lspLicenseAttaches) {
                        attachDetailVOS.addAll(addAttachUploadResult(bindLspAttach(lspLicenseAttach)));
                    }
                }
            }
            //司机
            else if (DRIVER.equals(licenseVO.getBelongedType())) {
                //查找证照信息
                EntityWrapper<DriverLicense> driverLicenseEntityWrapper = new EntityWrapper<>();
                driverLicenseEntityWrapper.eq("driver_id", licenseVO.getBelongedId()).eq("license_type_id", licenseVO.getLicenseTypeId());
                List<DriverLicense> driverLicenses = driverLicenseService.selectList(driverLicenseEntityWrapper);
                if (!CollectionUtils.isEmpty(driverLicenses)) {
                    //查找旗下的附件信息
                    EntityWrapper<DriverLicenseAttach> driverLicenseAttachEntityWrapper = new EntityWrapper<>();
                    driverLicenseAttachEntityWrapper.eq("license_id", driverLicenses.get(0).getId()).eq("driver_id", licenseVO.getBelongedId());
                    List<DriverLicenseAttach> driverLicenseAttaches = driverLicenseAttachService.selectList(driverLicenseAttachEntityWrapper);
                    //遍历附件，绑定文件附属信息
                    for (DriverLicenseAttach driverLicenseAttach :
                            driverLicenseAttaches) {
                        attachDetailVOS.addAll(addAttachUploadResult(bindDriverAttach(driverLicenseAttach)));
                    }
                }
            }
            //牵引车
            else if (CARRIER.equals(licenseVO.getBelongedType())) {
                //查找证照信息
                EntityWrapper<CarrierLicense> carrierLicenseEntityWrapper = new EntityWrapper<>();
                carrierLicenseEntityWrapper.eq("carrier_id", licenseVO.getBelongedId()).eq("license_type_id", licenseVO.getLicenseTypeId());
                List<CarrierLicense> carrierLicenses = carrierLicenseService.selectList(carrierLicenseEntityWrapper);
                if (!CollectionUtils.isEmpty(carrierLicenses)) {
                    //查找旗下的附件信息
                    EntityWrapper<CarrierLicenseAttach> carrierLicenseAttachEntityWrapper = new EntityWrapper<>();
                    carrierLicenseAttachEntityWrapper.eq("license_id", carrierLicenses.get(0).getId()).eq("carrier_id", licenseVO.getBelongedId());
                    List<CarrierLicenseAttach> carrierLicenseAttaches = carrierLicenseAttachService.selectList(carrierLicenseAttachEntityWrapper);
                    //遍历附件，绑定文件附属信息
                    for (CarrierLicenseAttach carrierLicenseAttach :
                            carrierLicenseAttaches) {
                        attachDetailVOS.addAll(addAttachUploadResult(bindCarrierAttach(carrierLicenseAttach)));
                    }
                }
            }
            //挂车
            else if (TRAILER.equals(licenseVO.getBelongedType())) {
                //查找证照信息
                EntityWrapper<TrailerLicense> trailerLicenseEntityWrapper = new EntityWrapper<>();
                trailerLicenseEntityWrapper.eq("trailer_id", licenseVO.getBelongedId()).eq("license_type_id", licenseVO.getLicenseTypeId());
                List<TrailerLicense> trailerLicenses = trailerLicenseService.selectList(trailerLicenseEntityWrapper);
                if (!CollectionUtils.isEmpty(trailerLicenses)) {
                    //查找旗下的附件信息
                    EntityWrapper<TrailerLicenseAttach> trailerLicenseAttachEntityWrapper = new EntityWrapper<>();
                    trailerLicenseAttachEntityWrapper.eq("license_id", trailerLicenses.get(0).getId()).eq("trailer_id", licenseVO.getBelongedId());
                    List<TrailerLicenseAttach> trailerLicenseAttaches = trailerLicenseAttachService.selectList(trailerLicenseAttachEntityWrapper);
                    //遍历附件，绑定文件附属信息
                    for (TrailerLicenseAttach trailerLicenseAttach :
                            trailerLicenseAttaches) {
                        attachDetailVOS.addAll(addAttachUploadResult(bindTrailerAttach(trailerLicenseAttach)));
                    }
                }
            } else {
                throw new BaseException("标识类型错误");
            }
        } catch (BaseException e) {
            LOGGER.error("LspInfoServiceImpl.queryAttachList" + e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("LspInfoServiceImpl.queryAttachList" + e.getMessage());
            throw new BaseException("获取附件异常");
        }
        return attachDetailVOS;
    }

    /**
     * 创建附件信息
     * @param attachDetailVO
     * @return
     */
    @Override
    @Transactional
    public List<AttachDetailVO> createAttach(AttachDetailVO attachDetailVO) {
        if (Objects.isNull(attachDetailVO.getBelongedId())) throw new BaseException("未指定所属类型");
        try {
            if (LSP.equals(attachDetailVO.getBelongedType())) {
                return this.createLspAttach(attachDetailVO);
            } else if (DRIVER.equals(attachDetailVO.getBelongedType())) {
                return this.createDriverAttach(attachDetailVO);
            } else if (CARRIER.equals(attachDetailVO.getBelongedType())) {
                return this.createCarrierAttach(attachDetailVO);
            } else if (TRAILER.equals(attachDetailVO.getBelongedType())) {
                return this.createTrailerAttach(attachDetailVO);
            } else {
                throw new BaseException("标识类型错误");
            }
        } catch (BaseException e) {
            LOGGER.error("LspInfoServiceImpl.createAttach" + e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("LspInfoServiceImpl.createAttach" + e.getMessage());
            throw new BaseException("上传附件信息错误");
        }
    }

    /**
     * 根据id号和标识类型删除指定附件
     * @param attachDetailVO
     */
    @Override
    public void deleteAttach(AttachDetailVO attachDetailVO) {
        if (Objects.isNull(attachDetailVO.getAttachId())) throw new BaseException("未获取到对应序号");
        try {
            if (LSP.equals(attachDetailVO.getBelongedType())) {
                LspLicenseAttach licenseAttach = lspLicenseAttachService.selectById(attachDetailVO.getAttachId());
                if (Objects.isNull(licenseAttach)) throw new BaseException("未找到对应附件");
                lspLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
            } else if (CARRIER.equals(attachDetailVO.getBelongedType())) {
                CarrierLicenseAttach licenseAttach = carrierLicenseAttachService.selectById(attachDetailVO.getAttachId());
                if (Objects.isNull(licenseAttach)) throw new BaseException("未找到对应附件");
                carrierLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
            } else if (DRIVER.equals(attachDetailVO.getBelongedType())) {
                DriverLicenseAttach licenseAttach = driverLicenseAttachService.selectById(attachDetailVO.getAttachId());
                if (Objects.isNull(licenseAttach)) throw new BaseException("未找到对应附件");
                driverLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
            } else if (TRAILER.equals(attachDetailVO.getBelongedType())) {
                TrailerLicenseAttach licenseAttach = trailerLicenseAttachService.selectById(attachDetailVO.getAttachId());
                if (Objects.isNull(licenseAttach)) throw new BaseException("未找到对应附件");
                trailerLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
            } else {
                throw new BaseException("标识类型错误");
            }
        } catch (BaseException e) {
            LOGGER.error("LspInfoServiceImpl.deleteAttach" + e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("LspInfoServiceImpl.deleteAttach" + e.getMessage());
            throw new BaseException("删除附件信息异常");
        }
    }


    /**
     * 增加绑定文件数据
     *
     * @param attachDetailVO
     * @return
     */
    private List<AttachDetailVO> addAttachUploadResult(AttachDetailVO attachDetailVO) {
        //下载的文件信息
        String key = attachDetailVO.getAttachKey();
        String lookUrl = QiniuUtils.generateDownloadTicket(qiniuConfig.getDownloadUrl(), key);
        attachDetailVO.setLookUrl(lookUrl);
        try {
            attachDetailVO.setFormat(QiniuUtils.getType(key));
        } catch (Exception e) {
            LOGGER.error("获取文件格式异常...", e);
        }
        // 下载地址
        String downloadUrl = lookUrl + "&attname=" + attachDetailVO.getAttachName();
        attachDetailVO.setDownloadUrl(downloadUrl);

        try {
            attachDetailVO.setSize(QiniuUtils.getSize(key));
        } catch (Exception e) {
            LOGGER.error("获取文件大小异常...", e);
        }

        // 文件上传时间
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long time = QiniuUtils.getPutTime(key);
            if (time != null) {
                String timeStr = time.toString().substring(0, 10);
                attachDetailVO.setUploadTime(simpleDateFormat.format(new Date(Long.valueOf(timeStr + "000"))));
            }
        } catch (Exception e) {
            LOGGER.error("获取文件上传时间异常...", e);
        }
        List<AttachDetailVO> attachDetailVOs = new ArrayList<AttachDetailVO>();
        attachDetailVOs.add(attachDetailVO);
        return attachDetailVOs;
    }

    //附件信息创建方法
    /**
     * 创建承运商附件
     * @param attachDetailVO
     * @return
     */
    private List<AttachDetailVO> createLspAttach(AttachDetailVO attachDetailVO){
        //绑定性参数
        JwtAccountVO jwtAccount = AccountUtil.getAccountInfoFromSecurityContext();
        LspLicense lspLicense = new LspLicense();
        //获取证照信息
        EntityWrapper<LspLicense> licenseEntityWrapper = new EntityWrapper<>();
        licenseEntityWrapper.eq("lsp_id", attachDetailVO.getBelongedId()).eq("license_type_id", attachDetailVO.getLicenseTypeId());
        List<LspLicense> lspLicenses = lspLicenseService.selectList(licenseEntityWrapper);
        //判断证照类型是否存在
        if (CollectionUtils.isEmpty(lspLicenses)) {
            //如果没有该类型证照，新增证照创建id
            lspLicense.setLspId(attachDetailVO.getBelongedId());
            lspLicense.setLicenseTypeId(attachDetailVO.getLicenseTypeId());
            lspLicense.setGmtCreate(new Date());
            if (Objects.nonNull(jwtAccount)) {
                lspLicense.setCreator(jwtAccount.getAccountId());
                lspLicense.setTenantId(Long.parseLong(jwtAccount.getTenantId()));
            }
            lspLicenseService.insert(lspLicense);
        } else {
            //如果有该类型证照，更新附件数据
            lspLicense = lspLicenses.get(0);
            EntityWrapper<LspLicenseAttach> ewAttach = new EntityWrapper<>();
            ewAttach.eq("lsp_id", attachDetailVO.getBelongedId());
            ewAttach.eq("license_id", lspLicense.getId());
            ewAttach.eq("attach_type", attachDetailVO.getAttachType());
            //获取对应承运商现有证照的附件信息
            LspLicenseAttach licenseAttach = lspLicenseAttachService.selectOne(ewAttach);
            //若存在附件
            if (Objects.nonNull(licenseAttach)) {
                //和本次上传的附件不一样，软删之后在新增，不一样直接跳过本次新增
                if (!attachDetailVO.getAttachKey().equals(licenseAttach.getAttachKey())) {
                    lspLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
                } else {
                    return this.addAttachUploadResult(bindLspAttach(licenseAttach));
                }
            }
        }
        //绑定剩余参数并进行新增
        LspLicenseAttach lspLicenseAttach = new LspLicenseAttach();
        lspLicenseAttach.setAttachName(attachDetailVO.getAttachName());
        lspLicenseAttach.setAttachType(attachDetailVO.getAttachType());
        lspLicenseAttach.setLicenseId(lspLicense.getId());
        lspLicenseAttach.setAttachKey(attachDetailVO.getAttachKey());
        lspLicenseAttach.setLspId(attachDetailVO.getBelongedId());
        lspLicenseAttach.setCreator(jwtAccount.getUsername());
        lspLicenseAttachService.insert(lspLicenseAttach);
        //绑定文件信息返回
        return this.addAttachUploadResult(bindLspAttach(lspLicenseAttach));
    }
    /**
     * 创建牵引车附件
     * @param attachDetailVO
     * @return
     */
    private List<AttachDetailVO> createCarrierAttach(AttachDetailVO attachDetailVO){
        JwtAccountVO jwtAccount = AccountUtil.getAccountInfoFromSecurityContext();
        //绑定性参数
        CarrierLicense carrierLicense = new CarrierLicense();
        //获取证照信息
        EntityWrapper<CarrierLicense> licenseEntityWrapper = new EntityWrapper<>();
        licenseEntityWrapper.eq("carrier_id", attachDetailVO.getBelongedId()).eq("license_type_id", attachDetailVO.getLicenseTypeId());
        List<CarrierLicense> carrierLicenses = carrierLicenseService.selectList(licenseEntityWrapper);
        //判断证照类型是否存在
        if (CollectionUtils.isEmpty(carrierLicenses)) {
            //如果没有该类型证照，新增证照创建id
            carrierLicense.setCarrierId(attachDetailVO.getBelongedId());
            carrierLicense.setLicenseTypeId(attachDetailVO.getLicenseTypeId());
            carrierLicenseService.insert(carrierLicense);
        } else {
            //如果有该类型证照，更新附件数据
            carrierLicense = carrierLicenses.get(0);
            EntityWrapper<CarrierLicenseAttach> ewAttach = new EntityWrapper<>();
            ewAttach.eq("carrier_id", attachDetailVO.getBelongedId());
            ewAttach.eq("license_id", carrierLicense.getId());
            ewAttach.eq("attach_type", attachDetailVO.getAttachType());

            if(11 == attachDetailVO.getLicenseTypeId()){// 牵引车照片
                ewAttach.eq("attach_key",attachDetailVO.getAttachKey());
                CarrierLicenseAttach licenseAttach = carrierLicenseAttachService.selectOne(ewAttach);
                if(null == licenseAttach){
                    CarrierLicenseAttach carrierLicenseAttach = addCarrierLicenseAttach(attachDetailVO, jwtAccount, carrierLicense);
                    //绑定文件信息返回
                    return this.addAttachUploadResult(bindCarrierAttach(carrierLicenseAttach));
                }
                // 存在返回之前文件信息
                LicenseVO licenseVO = new LicenseVO();
                BeanUtils.copyProperties(attachDetailVO, licenseVO);
                return queryAttachList(licenseVO);
            }
            //获取对应承运商现有证照的附件信息
            CarrierLicenseAttach licenseAttach = carrierLicenseAttachService.selectOne(ewAttach);
            //若存在附件
            if (Objects.nonNull(licenseAttach)) {
                //和本次上传的附件不一样，软删之后在新增，不一样直接跳过本次新增
                if (!attachDetailVO.getAttachKey().equals(licenseAttach.getAttachKey())) {
                    carrierLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
                } else {
                    return this.addAttachUploadResult(bindCarrierAttach(licenseAttach));
                }
            }
        }
        //绑定剩余参数并进行新增
        CarrierLicenseAttach carrierLicenseAttach = addCarrierLicenseAttach(attachDetailVO, jwtAccount, carrierLicense);
        //绑定文件信息返回
        return this.addAttachUploadResult(bindCarrierAttach(carrierLicenseAttach));
    }

    private CarrierLicenseAttach addCarrierLicenseAttach(AttachDetailVO attachDetailVO, JwtAccountVO jwtAccount, CarrierLicense carrierLicense) {
        CarrierLicenseAttach carrierLicenseAttach = new CarrierLicenseAttach();
        carrierLicenseAttach.setAttachName(attachDetailVO.getAttachName());
        carrierLicenseAttach.setAttachType(attachDetailVO.getAttachType());
        carrierLicenseAttach.setLicenseId(carrierLicense.getId());
        carrierLicenseAttach.setAttachKey(attachDetailVO.getAttachKey());
        carrierLicenseAttach.setCarrierId(attachDetailVO.getBelongedId());
        carrierLicenseAttach.setCreator(jwtAccount.getUsername());
        carrierLicenseAttachService.insert(carrierLicenseAttach);
        return carrierLicenseAttach;
    }

    /**
     * 创建挂车附件
     * @param attachDetailVO
     * @return
     */
    private List<AttachDetailVO> createTrailerAttach(AttachDetailVO attachDetailVO){
        JwtAccountVO jwtAccount = AccountUtil.getAccountInfoFromSecurityContext();
        //绑定性参数
        TrailerLicense trailerLicense = new TrailerLicense();
        //获取证照信息
        EntityWrapper<TrailerLicense> licenseEntityWrapper = new EntityWrapper<>();
        licenseEntityWrapper.eq("trailer_id", attachDetailVO.getBelongedId()).eq("license_type_id", attachDetailVO.getLicenseTypeId());
        List<TrailerLicense> trailerLicenses = trailerLicenseService.selectList(licenseEntityWrapper);
        //判断证照类型是否存在
        if (CollectionUtils.isEmpty(trailerLicenses)) {
            //如果没有该类型证照，新增证照创建id
            trailerLicense.setTrailerId(attachDetailVO.getBelongedId());
            trailerLicense.setLicenseTypeId(attachDetailVO.getLicenseTypeId());
            trailerLicenseService.insert(trailerLicense);
        } else {
            //如果有该类型证照，更新附件数据
            trailerLicense = trailerLicenses.get(0);
            EntityWrapper<TrailerLicenseAttach> ewAttach = new EntityWrapper<>();
            ewAttach.eq("trailer_id", attachDetailVO.getBelongedId());
            ewAttach.eq("license_id", trailerLicense.getId());
            ewAttach.eq("attach_type", attachDetailVO.getAttachType());

            if(12 == attachDetailVO.getLicenseTypeId()){// 挂车照片
                ewAttach.eq("attach_key",attachDetailVO.getAttachKey());
                TrailerLicenseAttach licenseAttach = trailerLicenseAttachService.selectOne(ewAttach);
                if(null == licenseAttach){
                    TrailerLicenseAttach trailerLicenseAttach = addTrailerLicenseAttach(attachDetailVO, jwtAccount, trailerLicense);
                    //绑定文件信息返回
                    return this.addAttachUploadResult(bindTrailerAttach(trailerLicenseAttach));
                }
                // 存在返回之前文件信息
                LicenseVO licenseVO = new LicenseVO();
                BeanUtils.copyProperties(attachDetailVO, licenseVO);
                return queryAttachList(licenseVO);
            }

            //获取对应承运商现有证照的附件信息
            TrailerLicenseAttach licenseAttach = trailerLicenseAttachService.selectOne(ewAttach);
            //若存在附件
            if (Objects.nonNull(licenseAttach)) {
                //和本次上传的附件不一样，软删之后在新增，不一样直接跳过本次新增
                if (!attachDetailVO.getAttachKey().equals(licenseAttach.getAttachKey())) {
                    trailerLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
                } else {
                    return this.addAttachUploadResult(bindTrailerAttach(licenseAttach));
                }
            }
        }
        //绑定剩余参数并进行新增
        TrailerLicenseAttach trailerLicenseAttach = addTrailerLicenseAttach(attachDetailVO, jwtAccount, trailerLicense);
        //绑定文件信息返回
        return this.addAttachUploadResult(bindTrailerAttach(trailerLicenseAttach));}

    private TrailerLicenseAttach addTrailerLicenseAttach(AttachDetailVO attachDetailVO, JwtAccountVO jwtAccount, TrailerLicense trailerLicense) {
        TrailerLicenseAttach trailerLicenseAttach = new TrailerLicenseAttach();
        trailerLicenseAttach.setAttachName(attachDetailVO.getAttachName());
        trailerLicenseAttach.setAttachType(attachDetailVO.getAttachType());
        trailerLicenseAttach.setLicenseId(trailerLicense.getId());
        trailerLicenseAttach.setAttachKey(attachDetailVO.getAttachKey());
        trailerLicenseAttach.setTrailerId(attachDetailVO.getBelongedId());
        trailerLicenseAttach.setCreator(jwtAccount.getUsername());
        trailerLicenseAttachService.insert(trailerLicenseAttach);
        return trailerLicenseAttach;
    }

    /**
     * 创建司机附件
     * @param attachDetailVO
     * @return
     */
    private List<AttachDetailVO> createDriverAttach(AttachDetailVO attachDetailVO){
        JwtAccountVO jwtAccount = AccountUtil.getAccountInfoFromSecurityContext();
        //绑定性参数
        DriverLicense driverLicense = new DriverLicense();

        //获取证照信息
        EntityWrapper<DriverLicense> licenseEntityWrapper = new EntityWrapper<>();
        licenseEntityWrapper.eq("driver_id", attachDetailVO.getBelongedId()).eq("license_type_id", attachDetailVO.getLicenseTypeId());
        List<DriverLicense> driverLicenses = driverLicenseService.selectList(licenseEntityWrapper);
        //判断证照类型是否存在
        if (CollectionUtils.isEmpty(driverLicenses)) {
            //如果没有该类型证照，新增证照创建id
            driverLicense.setDriverId(attachDetailVO.getBelongedId());
            driverLicense.setLicenseTypeId(attachDetailVO.getLicenseTypeId());
            driverLicenseService.insert(driverLicense);
        } else {
            //如果有该类型证照，更新附件数据
            driverLicense = driverLicenses.get(0);
            EntityWrapper<DriverLicenseAttach> ewAttach = new EntityWrapper<>();
            ewAttach.eq("driver_id", attachDetailVO.getBelongedId());
            ewAttach.eq("license_id", driverLicense.getId());
            ewAttach.eq("attach_type", attachDetailVO.getAttachType());
            //获取对应承运商现有证照的附件信息
            DriverLicenseAttach licenseAttach = driverLicenseAttachService.selectOne(ewAttach);
            //若存在附件
            if (Objects.nonNull(licenseAttach)) {
                //和本次上传的附件不一样，软删之后在新增，不一样直接跳过本次新增
                if (!attachDetailVO.getAttachKey().equals(licenseAttach.getAttachKey())) {
                    driverLicenseAttachService.updateOldAttachDeleteState(licenseAttach);
                } else {
                    return this.addAttachUploadResult(bindDriverAttach(licenseAttach));
                }
            }
        }
        //绑定剩余参数并进行新增
        DriverLicenseAttach driverLicenseAttach = new DriverLicenseAttach();
        driverLicenseAttach.setAttachName(attachDetailVO.getAttachName());
        driverLicenseAttach.setAttachType(attachDetailVO.getAttachType());
        driverLicenseAttach.setLicenseId(driverLicense.getId());
        driverLicenseAttach.setAttachKey(attachDetailVO.getAttachKey());
        driverLicenseAttach.setDriverId(attachDetailVO.getBelongedId());
        driverLicenseAttach.setCreator(jwtAccount.getUsername());
        driverLicenseAttachService.insert(driverLicenseAttach);
        //绑定文件信息返回
        return this.addAttachUploadResult(bindDriverAttach(driverLicenseAttach));
    }

    //属性值转换的方法
    private AttachDetailVO bindLspAttach(LspLicenseAttach lspLicenseAttach) {
        AttachDetailVO bindattach = new AttachDetailVO();
        bindattach.setAttachId(lspLicenseAttach.getId());
        bindattach.setAttachName(lspLicenseAttach.getAttachName());
        bindattach.setAttachType(lspLicenseAttach.getAttachType());
        bindattach.setLicenseId(lspLicenseAttach.getLicenseId());
        bindattach.setBelongedId(lspLicenseAttach.getLspId());
        bindattach.setAttachKey(lspLicenseAttach.getAttachKey());
        return bindattach;
    }

    private AttachDetailVO bindCarrierAttach(CarrierLicenseAttach carrierLicenseAttach) {
        AttachDetailVO bindattach = new AttachDetailVO();
        bindattach.setAttachId(carrierLicenseAttach.getId());
        bindattach.setAttachName(carrierLicenseAttach.getAttachName());
        bindattach.setAttachType(carrierLicenseAttach.getAttachType());
        bindattach.setLicenseId(carrierLicenseAttach.getLicenseId());
        bindattach.setBelongedId(carrierLicenseAttach.getCarrierId());
        bindattach.setAttachKey(carrierLicenseAttach.getAttachKey());
        return bindattach;
    }

    private AttachDetailVO bindDriverAttach(DriverLicenseAttach driverLicenseAttach) {
        AttachDetailVO bindattach = new AttachDetailVO();
        bindattach.setAttachId(driverLicenseAttach.getId());
        bindattach.setAttachName(driverLicenseAttach.getAttachName());
        bindattach.setAttachType(driverLicenseAttach.getAttachType());
        bindattach.setLicenseId(driverLicenseAttach.getLicenseId());
        bindattach.setBelongedId(driverLicenseAttach.getDriverId());
        bindattach.setAttachKey(driverLicenseAttach.getAttachKey());
        return bindattach;
    }

    private AttachDetailVO bindTrailerAttach(TrailerLicenseAttach trailerLicenseAttach) {
        AttachDetailVO bindattach = new AttachDetailVO();
        bindattach.setAttachId(trailerLicenseAttach.getId());
        bindattach.setAttachName(trailerLicenseAttach.getAttachName());
        bindattach.setAttachType(trailerLicenseAttach.getAttachType());
        bindattach.setLicenseId(trailerLicenseAttach.getLicenseId());
        bindattach.setBelongedId(trailerLicenseAttach.getTrailerId());
        bindattach.setAttachKey(trailerLicenseAttach.getAttachKey());
        return bindattach;
    }
}
